package naga.x.platform2.channel.iapppay.support;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.beans.factory.InitializingBean;

import naga.x.platform2.utils.NumberUtils;
import naga.x.platform2.utils.StringUtils;

public abstract class BaseIAppPayConfig implements InitializingBean {

	private String appId;
	private String privateKey;
	private String publicKey;
	private String loginCheckUrl; // 登录验证接口
	private String placeOrderUrl; // 下订单接口
	private String payUrl; // 前端支付页面
	private String payCallbackUrl; // 支付回调
	private String payFinishRedirectUrl; // 支付完成后客户端跳转地址
	private String payCancelRedirectUrl; // 支付取消后客户端跳转地址
	private String goods; // 商品表(productId1=goodsId1,productId2=goodsId2,...)
	private Map<Integer, Integer> goodsMap;

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public String getPrivateKey() {
		return privateKey;
	}

	public void setPrivateKey(String privateKey) {
		this.privateKey = privateKey;
	}

	public String getPublicKey() {
		return publicKey;
	}

	public void setPublicKey(String publicKey) {
		this.publicKey = publicKey;
	}

	public String getLoginCheckUrl() {
		return loginCheckUrl;
	}

	public void setLoginCheckUrl(String loginCheckUrl) {
		this.loginCheckUrl = loginCheckUrl;
	}

	public String getPlaceOrderUrl() {
		return placeOrderUrl;
	}

	public void setPlaceOrderUrl(String placeOrderUrl) {
		this.placeOrderUrl = placeOrderUrl;
	}

	public String getPayUrl() {
		return payUrl;
	}

	public void setPayUrl(String payUrl) {
		this.payUrl = payUrl;
	}

	public String getPayCallbackUrl() {
		return payCallbackUrl;
	}

	public void setPayCallbackUrl(String payCallbackUrl) {
		this.payCallbackUrl = payCallbackUrl;
	}

	public String getPayFinishRedirectUrl() {
		return payFinishRedirectUrl;
	}

	public void setPayFinishRedirectUrl(String payFinishRedirectUrl) {
		this.payFinishRedirectUrl = payFinishRedirectUrl;
	}

	public String getPayCancelRedirectUrl() {
		return payCancelRedirectUrl;
	}

	public void setPayCancelRedirectUrl(String payCancelRedirectUrl) {
		this.payCancelRedirectUrl = payCancelRedirectUrl;
	}

	public String getGoods() {
		return goods;
	}

	public void setGoods(String goods) {
		this.goods = goods;
	}

	public Integer getGoodsId(Integer productId) {
		return goodsMap.get(productId);
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		parseGoods();
	}
	
	private void parseGoods() {
		if (StringUtils.isEmpty(goods)) {
			goodsMap = Collections.emptyMap();
			return;
		}
		goodsMap = new LinkedHashMap<>();
		String[] strs = StringUtils.split(goods, ",");
		for (String str : strs) {
			int n = str.indexOf('=');
			int productId = NumberUtils.parseInt(StringUtils.trim(str.substring(0, n)));
			int goodsId = NumberUtils.parseInt(StringUtils.trim(str.substring(n + 1)));
			goodsMap.put(productId, goodsId);
		}
	}

	@Override
	public String toString() {
		return "[appId=" + appId + ", privateKey=" + /*privateKey*/"" + ", publicKey=" + /*publicKey*/""
				+ ", loginCheckUrl=" + loginCheckUrl + ", placeOrderUrl=" + placeOrderUrl + ", payUrl=" + payUrl
				+ ", payCallbackUrl=" + payCallbackUrl + ", payFinishRedirectUrl=" + payFinishRedirectUrl
				+ ", payCancelRedirectUrl=" + payCancelRedirectUrl + ", goodsMap=" + goodsMap + "]";
	}

}
